package com.aaa.car.controller;

import com.aaa.car.entity.TbCarType;
import com.aaa.car.entity.TbCarTypeConfigDetails;
import com.aaa.car.service.TbCarTypeService;
import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 车辆类型表(TbCarType)表控制层
 *
 * @author makejava
 * @since 2020-11-20 23:08:53
 */
@RestController
@RequestMapping("tbCarType")
public class TbCarTypeController {
    /**
     * 服务对象
     */
    @Resource
    private TbCarTypeService tbCarTypeService;

    /**
     * 查询车辆类型
     * @param pageNo
     * @param pageSize
     * @param tbCarType
     * @return
     */
    @GetMapping("queryCarType")
    public Result queryCarType(Integer pageNo, Integer pageSize,TbCarType tbCarType){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return tbCarTypeService.queryCarType(pageNo,pageSize,tbCarType);
    }
    /**
     * 修改车辆类型
     * @param tbCarType
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody TbCarType tbCarType){
        //当ID空为时抛出自定义异常
        if (tbCarType.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.tbCarTypeService.update(tbCarType));
    }

    /**
     * 添加车辆类型
     *
     * @param tbCarType
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody TbCarType tbCarType) {
        //判断新增的角色名字为空抛出异常
        if (tbCarType.getTypeName() == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        } else {
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.tbCarTypeService.insert(tbCarType));
        }
    }
    /**
     * 删除车辆类型
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(@Param("id") Integer id){
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.tbCarTypeService.deleteById(id));
    }

}